package dcc.component;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableCellRenderer;

import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import dcc.mbg.model.BaseInfo;
import dcc.service.CommonService;
import dcc.tools.BeanFactory;
import dcc.tools.Tools;

public class DataExportTable extends JTable {

	private String telentsLabel;
	private DataExportTaskPaneGroup taskPaneGroup;

	public DataExportTable(DataExportTaskPaneGroup taskPaneGroup,
			String telentsLabel) {
		super();
		this.taskPaneGroup = taskPaneGroup;
		this.telentsLabel = telentsLabel;
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
		this.setRowHeight(24);
		this.getTableHeader().setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
		Tools.get().addDataExportTable(this);
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (1 == e.getClickCount()) {
					int rowIndex = getSelectedRow();
					if (-1 == rowIndex) {
						return;
					} else {
						getModel()
								.setValueAt(
										!(Boolean.parseBoolean((getModel()
												.getValueAt(rowIndex, 0)
												.toString()))), rowIndex, 0);
					}
				}
			}
		});
	}

	private List<BaseInfoExtend> baseInfoExtendList;

	public void updateTable() {
		List<BaseInfo> baseInfoList = ((CommonService) BeanFactory
				.getBean("commonService"))
				.getBaseInfoByLeiBie(this.telentsLabel);
		baseInfoExtendList = new ArrayList<BaseInfoExtend>();
		for (BaseInfo baseInfo : baseInfoList) {
			baseInfoExtendList.add(new BaseInfoExtend(baseInfo));
		}
		initDataBindings(baseInfoExtendList);
		if (baseInfoList.size() == 0) {
			taskPaneGroup.setVisible(false);
		} else {
			taskPaneGroup.setVisible(true);
		}
	}

	public void selectAllTalents() {
		for (int i = 0; i < this.getRowCount(); i++) {
			getModel().setValueAt(true, i, 0);
		}
	}

	public void unSelectAllTalents() {
		for (int i = 0; i < this.getRowCount(); i++) {
			getModel().setValueAt(false, i, 0);
		}
	}

	public List<BaseInfo> getSelectedTalents() {
		List<BaseInfo> selectedTalents = new ArrayList<BaseInfo>();
		for (BaseInfoExtend baseInfoExtend : baseInfoExtendList) {
			if (baseInfoExtend.getChecked()) {
				selectedTalents.add(baseInfoExtend.getBaseInfo());
			}
		}
		return selectedTalents;
	}

	protected void initDataBindings(List<BaseInfoExtend> baseInfoExtendList) {
		JTableBinding<BaseInfoExtend, List<BaseInfoExtend>, JTable> jTableBinding = SwingBindings
				.createJTableBinding(UpdateStrategy.READ, baseInfoExtendList,
						this);
		//
		BeanProperty<BaseInfoExtend, Boolean> baseInfoExtendBeanProperty = BeanProperty
				.create("checked");
		jTableBinding.addColumnBinding(baseInfoExtendBeanProperty)
				.setColumnName("\u9009\u62E9").setEditable(false);
		//
		BeanProperty<BaseInfoExtend, String> leiBie = BeanProperty
				.create("baseInfo.leiBie");
		jTableBinding.addColumnBinding(leiBie)
				.setColumnName("\u4EBA\u624D\u7C7B\u578B").setEditable(false);
		//
		BeanProperty<BaseInfoExtend, String> shiBieHao = BeanProperty
				.create("baseInfo.shiBieHao");
		jTableBinding.addColumnBinding(shiBieHao)
				.setColumnName("\u4EBA\u624D\u8BC6\u522B\u53F7")
				.setEditable(false);
		//
		BeanProperty<BaseInfoExtend, String> xingMing = BeanProperty
				.create("baseInfo.xingMing");
		jTableBinding.addColumnBinding(xingMing).setColumnName("\u59D3\u540D")
				.setEditable(false);
		//
		BeanProperty<BaseInfoExtend, String> xingBie = BeanProperty
				.create("baseInfo.xingBie");
		jTableBinding.addColumnBinding(xingBie).setColumnName("\u6027\u522B")
				.setEditable(false);
		//
		jTableBinding.bind();
		this.getColumnModel().getColumn(0).setMaxWidth(50);
		this.getColumnModel().getColumn(1).setMinWidth(120);
		this.getColumnModel().getColumn(2).setMinWidth(280);
		this.getColumnModel().getColumn(3).setMinWidth(140);
		getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
			@Override
			public Component getTableCellRendererComponent(JTable table,
					Object value, boolean isSelected, boolean hasFocus,
					int row, int column) {
				JCheckBox checkBox = new JCheckBox();
				checkBox.setSelected(Boolean.parseBoolean(value.toString()));
				return checkBox;
			}
		});
	}
}
